<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("menus.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Parts_Edit extends Page
{
       public $hfForm = null;
       public $lblDesc = null;
       public $edPartNo = null;
       public $edRef = null;
       public $Label4 = null;
       public $btnShowAccont = null;
       public $lblPrice = null;
       public $Label5 = null;
       public $radioInv = null;
       public $cmbCategory = null;
       public $Label3 = null;
       public $JTDate = null;
       public $Label2 = null;
       public $edAmt = null;
       public $Label1 = null;
       public $cmbAcct = null;
       public $lblAcct = null;
       public $chkWarranty = null;
       public $cmbVendor = null;
       public $lblVendor = null;
       public $lblPartNo = null;
       public $memoNotes = null;
       public $edPrice = null;
       public $edQty = null;
       public $lblNotes = null;
       public $lblQty = null;
       public $cmbPartNo = null;
       public $JSRoundDecimal = null;
       public $hfType = null;
       public $compid = null;
       public $JTJavaScript1 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript3 = null;
       public $JTDivAccount = null;
       public $JTGroupBox4 = null;
       public $btnAddAccount = null;
       public $btnDelAccount = null;
       public $edAccount = null;
       public $listAccount = null;
       public $hiddenCurrQuantity = null;
       public $btnCancel = null;
       public $btnUpdate = null;
       public $Image1 = null;
       public $hiddenWO = null;
       public $hiddenParts = null;
       public $hiddenVeh = null;
       public $JTSiteTheme1 = null;
       function cmbPartNoChange($sender, $params)
       {
       $parts_id = $this->cmbPartNo->getItemIndex();

       $query_veh_part = "SELECT * FROM veh_parts_tbl WHERE vehicle_id = '".$this->hiddenVeh->getValue()."' and part_num='".$parts_id."' order by parts_date DESC limit 1";
       $result_veh_part = mysql_query($query_veh_part) or die("SELECT VEH PART ".mysql_error());
       $row_veh_part = mysql_fetch_array($result_veh_part);

       $part_date = $row_veh_part['parts_date'];

       //die($part_date);

       $result_stat = mysql_query("SELECT * FROM invstocks_status_tbl WHERE stock_id='".$parts_id."'")
       or die("SELECT STAT ".mysql_error());
       $row_stat = mysql_fetch_array($result_stat);

       $span_num = $row_stat['span_num'];
       $span_unit = $row_stat['span_unit'];

       //die($span_num);

       if($span_unit=='months')
       {
       $date_computed = strtotime(date("Y-m-d", strtotime($part_date)) . "+".$span_num." month");
       }

       if($span_unit=='years')
       {
       $date_computed = strtotime(date("Y-m-d", strtotime($part_date)) . "+".$span_num." year");
       }

       $due_date = date("Y-m-d", $date_computed);
       $date_today = date('Y-m-d');
       //die($date_today.$due_date);

       if($date_today < $due_date)
       {
       echo '<script language=javascript>
       alert("current part used not reached max life span!");
       </script>';
       }



       }

       function Vehicle_Parts_EditJSLoad($sender, $params)
       {

       ?>
       //Add your javascript code here
       var r0=document.getElementById('radioInv_0').checked;
       var r1=document.getElementById('radioInv_1').checked;
       var lblpart=document.getElementById('lblPartNo_outer');
       var cmbpart=document.getElementById('cmbPartNo_outer')
       var lbldesc=document.getElementById('lblDesc_outer');
       var edpart=document.getElementById('edPartNo_outer');

       if(r0==true)
       {
       document.getElementById('lblPartNo_outer').style.visibility='visible';
       document.getElementById('cmbPartNo_outer').style.visibility='visible';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       document.getElementById('lblDesc_outer').style.visibility='hidden';
       document.getElementById('edPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       lblpart.style.left = '296px';
       cmbpart.style.left = '347px';
       lbldesc.style.left = '556px';
       edpart.style.left = '627px';
       }

       if(r1==true)
       {
       document.getElementById('lblPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbVendor_outer').style.visibility='hidden';
       document.getElementById('lblDesc_outer').style.visibility='visible';
       document.getElementById('edPartNo_outer').style.visibility='visible';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       lblpart.style.left = '556px';
       cmbpart.style.left = '627px';
       lbldesc.style.left = '276px';
       edpart.style.left = '347px';
       }
       <?php

       }


       function btnDelAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var AccountIndex = document.getElementById('listAccount').selectedIndex;
       var AccountValue = document.getElementById('listAccount')[AccountIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxAccount3(AccountValue,comp);
       <?php

       }

       function btnAddAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addaccount = document.getElementById('edAccount').value;
       var compider = document.getElementById('compid').value;
       AjaxAccount2(addaccount,compider);
       document.getElementById('edAccount').value = "";
       <?php

       }

       function btnShowAccontJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivAccount').Show();
       document.getElementBYId('edAccount').value = "";
       <?php

       }


       function radioInvJSChange($sender, $params)
       {
       //echo $this->radioInv->ajaxCall("processInv");
       ?>
       //Add your javascript code here
       var r0=document.getElementById('radioInv_0').checked;
       var r1=document.getElementById('radioInv_1').checked;
       var lblpart=document.getElementById('lblPartNo_outer');
       var cmbpart=document.getElementById('cmbPartNo_outer')
       var lbldesc=document.getElementById('lblDesc_outer');
       var edpart=document.getElementById('edPartNo_outer');

       if(r0==true)
       {
       document.getElementById('lblPartNo_outer').style.visibility='visible';
       document.getElementById('cmbPartNo_outer').style.visibility='visible';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       document.getElementById('lblDesc_outer').style.visibility='hidden';
       document.getElementById('edPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       lblpart.style.left = '296px';
       cmbpart.style.left = '347px';
       lbldesc.style.left = '556px';
       edpart.style.left = '627px';
       }

       if(r1==true)
       {
       document.getElementById('lblPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbPartNo_outer').style.visibility='hidden';
       document.getElementById('cmbVendor_outer').style.visibility='hidden';
       document.getElementById('lblDesc_outer').style.visibility='visible';
       document.getElementById('edPartNo_outer').style.visibility='visible';
       document.getElementById('cmbVendor_outer').style.visibility='visible';
       lblpart.style.left = '556px';
       cmbpart.style.left = '627px';
       lbldesc.style.left = '276px';
       edpart.style.left = '347px';
       }
       <?php

       }

       function btnCancelJSClick($sender, $params)
       {

       ?>
       window.close();
       <?php

       }

       function edPriceJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.Vehicle_Parts_Edit.edAmt.value = roundNumber((document.Vehicle_Parts_Edit.edQty.value) * (document.Vehicle_Parts_Edit.edPrice.value),2);


       //accept only numbers
       var numero = document.getElementById("edPrice").value;
       var first = numero.indexOf('.');
       var last = numero.lastIndexOf('.');

       while(isNaN(numero))
       {
        haba = numero.length;
        haba = haba - 1;
        numero = numero.substr(0,haba);
       }

       while(first!=last)
       {
        numero = numero.slice(0,last);
        first = numero.indexOf('.');
        last = numero.lastIndexOf('.');
       }
       document.getElementById("edPrice").value = numero;

       <?php

       }

       function edQtyJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.Vehicle_Parts_Edit.edAmt.value = roundNumber((document.Vehicle_Parts_Edit.edQty.value) * (document.Vehicle_Parts_Edit.edPrice.value),2);
       <?php

       }

       function btnUpdateClick($sender, $params)
       {
       $query_inv = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$this->cmbPartNo->getItemIndex()."'";
       $result_inv = mysql_query($query_inv) or die('INVENTORY - QUANTITY '.mysql_error());
       $row_inv = mysql_fetch_array($result_inv);

       $curr_qty = $row_inv['qty_available'];
       $qty_use = $this->edQty->Text;
       $curr_qty_used = $this->hiddenCurrQuantity->getValue();
       $added_avail_stock = $curr_qty_used + $curr_qty;

       $part_from = $this->radioInv->getItemIndex();
       if($curr_qty>=$qty_use || $part_from==1)
       {

       if($this->chkWarranty->Checked==true)
       {
       $warranty='1';
       }
       else
       {
       $warranty='0';
       }

       $parts_id = $this->cmbPartNo->getItemIndex();
       $result_parts = mysql_query("SELECT * FROM invstocks_tbl WHERE stock_id='".$parts_id."'")
       or die("SELECT PARTS ".mysql_error());
       $row_parts = mysql_fetch_array($result_parts);

       $stock_num = $row_parts['stock_num'];
       $stock_desc = $row_parts['description'];



       $query_update = "UPDATE veh_parts_tbl SET
       vendor='".$this->cmbVendor->getItemIndex()."',
       parts_date='".$this->JTDate->Text."',
       quantity='".$this->edQty->Text."',
       part_from='".$this->radioInv->getItemIndex()."',
       part_num='".$this->cmbPartNo->getItemIndex()."',
       other='".$this->edPartNo->Text."',
       reference_num='".$this->edRef->Text."',
       part_desc = '".$stock_num."-".$stock_desc."',
       category='".$this->cmbCategory->getItemIndex()."',
       price_ea='".$this->edPrice->Text."',
       amount='".$this->edAmt->Text."',
       account='".$this->cmbAcct->getItemIndex()."',
       notes='".$this->memoNotes->Text."',
       warranty='".$warranty."'
       WHERE workorder='".$this->hiddenWO->getValue()."' and parts_id ='".$this->hiddenParts->getValue()."'";

       $type = $this->hfType->getValue();
       $flag_up_inv_1 = 0;
       $flag_up_inv_2 = 0;
       if($this->hfForm->getValue()=='summary')
       {
        if($type=='create')
        {
        mysql_query($query_update) or die(mysql_error());
        }

        if($type=='edit')
        {
          $filename = "temp/parts_update.txt";
          $handle = fopen($filename, "w");
          fwrite($handle, $query_update);
          fclose($handle);
        }
       }
       if($this->hfForm->getValue()=='order')
       {
        mysql_query($query_update) or die(mysql_error());
       }

       $part_from = $this->radioInv->getItemIndex();
       if($part_from==0)
       {
       if($added_avail_stock>=$qty_use)
       {

        if($curr_qty_used>$qty_use)
        {
         $new_qty = $curr_qty + ($curr_qty_used-$qty_use);
        }
        if($curr_qty_used<$qty_use)
        {
         $new_qty = $curr_qty - ($qty_use-$curr_qty_used);
        }
        if($qty_use!=$curr_qty_used)
        {
        $query_up_inv = "UPDATE invstocks_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$this->cmbPartNo->getItemIndex()."'";
        $flag_up_inv_1 = 1;

        if($this->hfForm->getValue()=='summary')
        {
         if($type=='create')
         {
         mysql_query($query_up_inv) or die("UPDATE INV ".mysql_error());
         }
         if($type=='edit')
         {
         $filename = "temp/parts_inv_update_1.txt";
         $handle = fopen($filename, "w");
         fwrite($handle, $query_update);
         fclose($handle);
         }
        }
        if($this->hfForm->getValue()=='order')
        {
         mysql_query($query_up_inv) or die("UPDATE INV ".mysql_error());
        }

        $query_select_inv_stat = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$this->cmbPartNo->getItemIndex()."'";
        $result_select_inv_stat = mysql_query($query_select_inv_stat) or die("SELECT INV STAT ".mysql_error());
        $count_inv_stat = mysql_num_rows($result_select_inv_stat);

        if($count_inv_stat>0)
        {
         $query_up_inv = "UPDATE invstocks_status_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$this->cmbPartNo->getItemIndex()."'";
         $flag_up_inv_2 = 1;
         if($this->hfForm->getValue()=='summary')
         {
          if($type=='create')
          {
          mysql_query($query_up_inv) or die("UPDATE INV STATUS ".mysql_error());
          }
          if($type=='edit')
          {
          $filename = "temp/parts_inv_update_2.txt";
          $handle = fopen($filename, "w");
          fwrite($handle, $query_update);
          fclose($handle);
          }
         }
         if($this->hfForm->getValue()=='order')
         {
          mysql_query($query_up_inv) or die("UPDATE INV STATUS ".mysql_error());
         }
        }
        //die($new_qty.'xxx'.$query_up_inv.'curr_qty'.$curr_qty_used.'qty_use'.$qty_use);
        }
        if($this->hfForm->getValue()=='summary')
        {
         if($type=='create')
         {
         echo '<script language=javascript>
         window.opener.parent.setWODetails();
         window.opener.location.reload();
         alert("Successfully Updated Record!");
         window.close();
         </script>';
         }
         if($type=='edit')
          {
           echo "<script type=text/javascript>
           var wo = ".$this->hiddenWO->getValue().";
           var veh= ".$this->hiddenVeh->getValue()."
           var parts = ".$this->hiddenParts->getValue()."
           window.location.href='parts_endorsement.php?WO='+wo+'&veh='+veh+'&parts='+parts+'&flag1='+".$flag_up_inv_1."+'&flag2='+".$flag_up_inv_2.";
           </script>
          ";
          }
        }
        if($this->hfForm->getValue()=='order')
        {
         echo '<script language=javascript>
         window.opener.parent.setWODetails();
         window.opener.location.reload();
         alert("Successfully Updated Record!");
         window.close();
         </script>';
        }
       }
       }

       }

       if($curr_qty<$qty_use && $part_from==0)
       {
        echo '<script language=javascript>
        alert("Insufficient stock, you only have '.$curr_qty.' left.");
        </script>';
       }


       else
       {
       if($this->hfForm->getValue()=='summary')
        {
        if($type=='create')
        {
        echo '<script language=javascript>
         window.opener.parent.setWODetails();
         window.opener.location.reload();
         alert("Successfully Updated Record!");
         window.close();
         </script>';
        }
        if($type=='edit')
          {
           echo "<script type=text/javascript>
           var wo = ".$this->hiddenWO->getValue().";
           var veh= ".$this->hiddenVeh->getValue()."
           var parts = ".$this->hiddenParts->getValue()."
           window.location.href='parts_endorsement.php?WO='+wo+'&veh='+veh+'&parts='+parts;
           </script>
          ";
          }
        }
       if($this->hfForm->getValue()=='order')
       {
        echo '<script language=javascript>
         window.opener.parent.setWODetails();
         window.opener.location.reload();
         alert("Successfully Updated Record!");
         window.close();
         </script>';
       }
       }
       }


       function processInv()
       {
       $company_id = $_SESSION['company_id'];
       //type  0 = employee  1 = vendor
       //$this->cmbDismountWorkby->Clear();
       //$this->cmbDismountWorkby->AddItem('', null,'');

       $type = $this->radioInv->getItemIndex();
       if($type==0)
       {
       $this->edPartNo->Enabled=false;
       $this->edPartNo->Color=gray;
       $this->edPartNo->Text="";
       $this->cmbPartNo->Enabled=true;
       $this->cmbPartNo->Clear();
       $result_part = mysql_query("select * from invstocks_tbl WHERE company_id='".$_SESSION['company_id']."'");

       while($row_part = mysql_fetch_array($result_part))
       {
            $this->cmbPartNo->AddItem($row_part['stock_num'].' - '.$row_part['description'], null, $row_part['stock_id']);
       }
       }
       if($type==1)
       {
       $this->edPartNo->Enabled=true;
       $this->edPartNo->Color=white;
       $this->cmbPartNo->Enabled=false;
       }
       }




       function Vehicle_Parts_EditCreate($sender, $params)
       {
       $veh_id = $_GET['veh'];
       $veh_parts = $_GET['parts'];
       $WO = $_GET['WO'];
       if($veh_id!=null || $veh_id!='')
       $this->hiddenVeh->setValue($veh_id);
       if($WO!=null || $WO!='')
       $this->hiddenWO->setValue($WO);
       if($veh_parts!=null || $veh_parts!='')
       $this->hiddenParts->setValue($veh_parts);

       $type = $_GET['type'];
       if($type!=null || $type!='')
       $this->hfType->setValue($type);

       $form = $_GET['form'];
       if($form!=null || $form!='')
       $this->hfForm->setValue($form);

       //$this->radioInv->ItemIndex=0;
       if($_SESSION['access_vehicle']==1)
       {
       $this->btnCancel->Visible=false;
       $this->btnUpdate->Visible=false;
       }

       if($_SESSION['access_vehicle']==2)
       {
       $this->btnCancel->Visible=true;
       $this->btnUpdate->Visible=true;
       }



       $query1 = "select * from veh_parts_tbl where workorder='".$this->hiddenWO->getValue()."' and parts_id='".$this->hiddenParts->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);

       $part_from = $row['part_from'];
       if($part_from==0)
       {
       $this->radioInv->ItemIndex=0;
       $this->edPartNo->Enabled=false;
       $this->cmbPartNo->Enabled=true;
       $this->cmbPartNo->Clear();
       $this->cmbPartNo->ItemIndex=$row['part_num'];
       $result_part = mysql_query("select * from invstocks_tbl WHERE company_id='".$_SESSION['company_id']."'");

       while($row_part = mysql_fetch_array($result_part))
       {
            $this->cmbPartNo->AddItem($row_part['stock_num'].' - '.$row_part['description'], null, $row_part['stock_id']);
       }
       }

       if($part_from==1)
       {
       $this->radioInv->ItemIndex=1;
       $this->edPartNo->Enabled=true;
       $this->cmbPartNo->Enabled=false;
       $this->edPartNo->Text=$row['other'];
       }





       if($row['warranty']=="1")
       {
       $this->chkWarranty->Checked=true;
       }
       else
       {
       $this->chkWarranty->Checked=false;
       }


       $this->JTDate->Text=$row['parts_date'];
       $this->edQty->Text = $row['quantity'];
       $this->edRef->Text = $row['reference_num'];
       $this->hiddenCurrQuantity->setValue($row['quantity']);
       $this->edPrice->Text = $row['price_ea'];
       $this->edAmt->Text = $row['amount'];
       $this->memoNotes->Text = $row['notes'];
       $this->cmbCategory->ItemIndex = $row['category'];




       $this->cmbVendor->Clear();
       $this->cmbVendor->ItemIndex=$row['vendor'];
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbVendor->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }
       $this->cmbVendor->AddItem('Others', null, 'Others');

       $this->cmbAcct->Clear();
       $this->cmbAcct->ItemIndex=$row['account'];
        $result_account = mysql_query("select value from lookup_expense_tbl where company_id='".$_SESSION['company_id']."' AND category='accounts'");

        while($row_account = mysql_fetch_array($result_account))
        {
            $this->cmbAcct->AddItem($row_account['value'], null, $row_account['value']);
        }

       //list account
       $this->listAccount->Clear();
        $result_ac = mysql_query("select value from lookup_expense_tbl where company_id='".$_SESSION['company_id']."' AND category='accounts'");

        while($row_ac = mysql_fetch_array($result_ac))
        {
            $this->listAccount->AddItem($row_ac['value'], null, $row_ac['value']);
        }

       }



       function btnCancelClick($sender, $params)
       {
       redirect('veh_parts_list.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue());
       }



}

global $application;

global $Vehicle_Parts_Edit;

//Creates the form
$Vehicle_Parts_Edit=new Vehicle_Parts_Edit($application);

//Read from resource file
$Vehicle_Parts_Edit->loadResource(__FILE__);

//Shows the form
$Vehicle_Parts_Edit->show();

?>